﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using ThangNM_LIB.FUNC;

namespace ThangNM.PublicInterface
{
    public partial class Home_New : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            form1.Action = Request.RawUrl;
            try
            {
                UrlChangeEO obj = (new UrlChangeDAO()).SelectBy_sUrlSrc(OtherEx.RemoveDomainInUrl(Request.RawUrl));
                if (obj != null)
                {
                    HttpContext.Current.Response.Status = "301 Moved Permanently";
                    HttpContext.Current.Response.AddHeader("Location", OtherEx.AddDomainInUrl(obj.sUrlDes));
                }

                MsgUC1.setClearMsg(true);
                //MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "OK Welcome!!!");
                
                if (!IsPostBack)
                {
                    if (StringEx.Compare(Request.Url.ToString(), "www."))
                    {
                        Response.Status = "301 Moved Permanently";
                        Response.AddHeader("Location", System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + Request.RawUrl.Substring(1));
                        return;
                    }
                    if (Response.StatusCode != 301)
                    {
                        BindData();


                        //System.Web.UI.ClientScriptManager js3 = Page.ClientScript;
                        //if (!js3.IsClientScriptBlockRegistered(this.GetType(), "vdabc"))
                        //{
                        //    String jsText = "<script type=\"text/javascript\">setTimeout(function () {$(\".alert\").addClass('load_message_notification');$(\".alert\").fadeOut(10000); }, 8000);</script>";
                        //    js3.RegisterClientScriptBlock(this.GetType(), "vdabc", jsText.ToString(), false);
                        //}

                        
                    }
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            DataSet ds = (new TopicDAO()).getDataMasterPage();

            //=================================== New =====================
            //string sLinkByType = FileNameByFunction.Bai_Viet;
            //ds.Tables[0].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + dr["sUrlRewrite"].ToString();
            }
            rptTopicNew.DataSource = ds.Tables[0];
            rptTopicNew.DataBind();

            //sLinkByType = FileNameByFunction.Giai_Tri;
            //ds.Tables[1].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[1].Rows)
            {
                //sLinkByType = FileNameByFunction.Giai_Tri_ + GetTextConstants.Media_iType2sType_GTC(Convert.ToInt16(Commons.getDefaultValue(dr["FK_iCategoryID"], 0)));
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + dr["sUrlRewrite"].ToString();
            }
            rptMediaNew.DataSource = ds.Tables[1];
            rptMediaNew.DataBind();

            //sLinkByType = FileNameByFunction.Hoi_Dap;
            //ds.Tables[2].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[2].Rows)
            {
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(dr["sTitle"].ToString()) + "." + dr["PK_iSEO"].ToString() + FileNameByFunction.HTML;
            }
            rptQuestionNew.DataSource = ds.Tables[2];
            rptQuestionNew.DataBind();

            //=================================== Hot =====================
            //sLinkByType = FileNameByFunction.Bai_Viet;
            //ds.Tables[3].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[3].Rows)
            {
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + dr["sUrlRewrite"].ToString();
            }
            rptTopicHot.DataSource = ds.Tables[3];
            rptTopicHot.DataBind();

            //sLinkByType = FileNameByFunction.Giai_Tri;
            //ds.Tables[4].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[4].Rows)
            {
                //sLinkByType = FileNameByFunction.Giai_Tri_ + GetTextConstants.Media_iType2sType_GTC(Convert.ToInt16(Commons.getDefaultValue(dr["FK_iCategoryID"], 0)));
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + dr["sUrlRewrite"].ToString();
            }
            rptMediaHot.DataSource = ds.Tables[4];
            rptMediaHot.DataBind();

            //sLinkByType = FileNameByFunction.Hoi_Dap;
            //ds.Tables[5].Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            foreach (DataRow dr in ds.Tables[5].Rows)
            {
                dr["sUrlRewrite"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(dr["sTitle"].ToString()) + "." + dr["PK_iSEO"].ToString() + FileNameByFunction.HTML;
            }
            rptQuestionHot.DataSource = ds.Tables[5];
            rptQuestionHot.DataBind();

            //=================================== Tag =====================
            var lstTag = CustomerCache.get_lstCategory_ZipEO().FindAll(n => n.bState == true).OrderByDescending(x => x.iCount);
            String strHTML = "";
            foreach (var item in lstTag)
            {
                //if (item.iCount > 0)
                strHTML = strHTML + "<p class=\"btn icon-btn btn-info btn-sm link_color\" ><span class=\"btn-glyphicon img-circle\">" +
                        item.iCount + "</span><a href=\"" + OtherEx.AddDomainInUrl(item.sUrlRewrite) + "\" title=\"" + item.sName + "\">" +
                        item.sName + "</a></p>";
            }
            lblKeywork.Text = strHTML;

            //rptTag.DataSource = lstTag;
            //rptTag.DataBind();

            //=================================== Back Link =====================
            //List<BackLinkEO> lstBL = new List<BackLinkEO>();
            //string sBackLink = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/BackLink.xml"));
            //lstBL = (List<BackLinkEO>)XmlEx.Xml2ListObject(sBackLink, lstBL.GetType());
            //lstBL = lstBL.FindAll(x => x.iStatus == 1);
            //rptBackLink.DataSource = lstBL;
            //rptBackLink.DataBind();
        }

        public void setClearMsg()
        {
            MsgUC1.setClearMsg();
        }

        public void setNotifications(Int16 iType, string strMsg, String strMsgFull = null)
        {
            MsgUC1.setNotifications(iType, strMsg, strMsgFull);
        }

        //public void setPlaceHolder(System.Web.UI.Control ctlChild)
        //{
        //    if (cphCustomMenu != null && ctlChild != null)
        //    {
        //        cphCustomMenu.Controls.Clear();
        //        cphCustomMenu.Controls.Add(ctlChild);
        //        cphCustomMenu.Visible = true;
        //    }
        //}
    }
}